package com.chixing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.pojo.Evaluation;
import com.chixing.pojo.Pro;
import com.chixing.pojo.Product;
import com.chixing.service.EsProService;
import com.chixing.service.IEvaluationService;
import com.chixing.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class EsProController {

    @Autowired
    private EsProService esProService;

    @Autowired
    private IEvaluationService evaluationService;

    @Autowired
    private IProductService productService;

    @GetMapping("getProById/{id}")
    public   Optional<Pro> getProById(@PathVariable("id") Integer id){
        Optional<Pro> pro = esProService.findById(id);
        System.out.println("get by id:" + pro);
        return pro;
    }

    @GetMapping("getAllPro")
    public Iterable<Pro> getAllPro(){
        Iterable<Pro> pros = esProService.findAll();
        System.out.println("getall:"+pros);
        return pros;
    }

    @GetMapping("getByName")
    public ModelAndView getByName(@RequestParam("pageNum") Integer pageNum,@RequestParam("search")String search){
        System.out.println(search);
        System.out.println(pageNum);

        List<Pro> proList = esProService.findByproductBrand(search);
        if (proList.size()<=0){
            List<Product> productList = productService.getAll();
            for (Product product:productList){
                Pro pro = new Pro();
                pro.setId(product.getProductId());
                pro.setProductName(product.getProductName());
                pro.setProductType(product.getProductType());
                pro.setProductBrand(product.getProductBrand());
                pro.setProductPrice(product.getProductPrice());
                pro.setProductColor(product.getProductColor());
                pro.setProductSize(product.getProductSize());
                pro.setProductMainImg(product.getProductMainImg());
                pro.setProductCount(product.getProductCount());
                pro.setProductPoint(product.getProductPoint());
                pro.setProductArea(product.getProductArea());
                pro.setProductSold(product.getProductSold());
                pro.setXx(product.getXx());
                pro.setYy(product.getYy());
                pro.setProductVersion(product.getProductVersion());
                pro.setProductStatus(product.getProductStatus());
                esProService.save(pro);
            }
            proList = esProService.findByproductBrand(search);
        }
//        List<Integer> counts = new ArrayList<>();
        for (int i=0;i<proList.size();i++) {
            Pro pro = proList.get(i);
            QueryWrapper<Evaluation> wrapper = new QueryWrapper<>();
            wrapper.eq("product_id", pro.getId());
            List<Evaluation> evaluations = evaluationService.list(wrapper);
//            counts.add(evaluations.size());
            pro.setXx(String.valueOf(evaluations.size()));
            proList.set(i,pro);
        }
        System.out.println("abcdefg");
/*        Page<Pro> page = new Page<>(pageNum,4);
        Page<Pro> pageinfo  ;*/

        if (proList.size()-pageNum*4<=0) pageNum-=1;

        if (pageNum<1) pageNum=1;

        List<Pro> pros = new ArrayList<>();
        for (int i= (pageNum-1)*8;i< proList.size();i++){
            pros.add(proList.get(i));
        }

        System.out.println("hijklmn");


//        System.out.println(counts);
//        System.out.println(counts);
        int pagecount = (pros.size()-pros.size()%8)/8+1;
        ModelAndView mav = new ModelAndView();
        mav.addObject("pros",pros);
        mav.addObject("search",search);
        mav.addObject("count",pros.size());
        mav.addObject("pagecount",pagecount);
        mav.addObject("pageNum",pageNum);
        mav.setViewName("product_show/more_products.jsp");
        System.out.println(mav.getViewName());
        return mav;
    }


}
